<template>
  <div class="wrapper">
    <!--表单-->
    <div class="form">
      <div class="form-header">
        <img src="@/assets/logo.png" alt="logo" class="logo"/>
        <b style="color: #303133; font-weight: bold">人力资源管理系统</b>
        <!--实际表单-->
        <el-form :rules="rules" :model="staff" ref="loginForm">
          <el-form-item prop="code">
            <el-input size="medium" placeholder="请输入账号" style="margin: 10px 0" prefix-icon="el-icon-user"
                      v-model.trim="staff.code"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input size="medium" placeholder="请输入密码" style="margin: 10px 0" prefix-icon="el-icon-lock"
                      show-password
                      v-model.trim="staff.password"></el-input>
          </el-form-item>
          <div style="margin: 20px 0; text-align: center">
            <el-form-item>
              <el-button type="primary" size="medium" style="width:100%" @click="handleLogin">登 录</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {login} from '@/api/login'
export default {
  name: "index",
  data() {
    return {
      staff: {},
      // 效验规则
      rules: {
        code: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        console.log(this.staff)
        if (valid) {
          login(this.staff).then(response => {
            localStorage.setItem("staff", JSON.stringify(response.data)) // 存储用户信息到浏览器
            localStorage.setItem("token", response.token) // 存储token到浏览器
            this.$router.push({
              path: '/home'
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right, #FC466B, #3f5EFB);
  overflow: hidden;
}

.form {
  margin: 130px auto;
  background-color: #fff;
  width: 350px;
  height: 300px;
  padding: 20px;
  border-radius: 10px;
}

.form-header {
  height: 60px;
  line-height: 60px;
  text-align: center;
}

.logo {
  width: 20px;
  position: relative;
  top: 5px;
}
</style>
